import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:qaun_ni_dev/provider/admin_provide.dart';
import 'package:qaun_ni_dev/pages/adminManage/admin_detail.dart';
import 'package:qaun_ni_dev/pages/adminManage/admin_add.dart';


class AdminPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('管理员管理'),
        actions: <Widget>[
          new IconButton(
            icon: Icon(Icons.add),
            onPressed: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (context) => AdminAdd()));
            },
          ),

        ],
      ),
      body: Container(
        child: AdminList()
      ),
    );
  }
}


class AdminList extends StatefulWidget {
  @override
  _AdminListState createState() => _AdminListState();

}

class _AdminListState extends State<AdminList> {

  GlobalKey<RefreshFooterState> _footerKey = new GlobalKey<RefreshFooterState>();
  GlobalKey<RefreshHeaderState> _headerKey = new GlobalKey<RefreshHeaderState>();

  void initState() {
    super.initState();
    _getMoreList();
  }

  void _getMoreList() {
    Provider.of<AdminProvide>(context,listen: false).getAdminDataList(context);
  }


  @override
  Widget build(BuildContext context) {
    print('啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊');
    return Container(
      child: Consumer<AdminProvide>(
          builder: (context,data,_){
            if(data.adminDataList.length >0 ) {
              return Container(
                padding: EdgeInsets.only(top: 10,left: 15,right: 15),
                child: EasyRefresh(
                  refreshFooter: ClassicsFooter(
                      key:_footerKey,
                      bgColor:Colors.white,
                      textColor:Colors.pink,
                      moreInfoColor: Colors.pink,
                      showMore:true,
                      noMoreText: '没有更多了',
                      moreInfo:'加载中',
                      loadReadyText:'上拉加载'
                  ),
                  refreshHeader:ClassicsHeader(
                    key: _headerKey,
                      refreshText: "下拉刷新数据",
                      refreshReadyText: "准备刷新数据",
                      refreshingText: "数据刷新中...",
                      refreshedText: "刷新完成",
                      bgColor: Colors.blue,
                      textColor: Colors.white,
                      moreInfoColor: Colors.white,
                      refreshHeight: 70.0,
                      isFloat: false,
                     showMore: false,
                      moreInfo: "Updated at %T"
                  ),
                  child: ListView.builder(
                    itemCount: data.adminDataList.length,
                      itemBuilder: (context,index) {
                        return _listWidget(data.adminDataList,index);
                      }
                  ),
                  loadMore: () async {
                    print('加载更多的');
                    Provider.of<AdminProvide>(context,listen: false).getAdminDataMoreList(context);
                  },
                    onRefresh: () async{
                      print('上拉刷新');
                      Provider.of<AdminProvide>(context,listen: false).onRefreshList(context);
                    }
                ),
              );
            } else {
              return Text('暂无数据哦，请稍等');
            }
          }
      ),
    );
  }
  Widget _listWidget (List newList,int index) {
    return InkWell(
      onTap: () {
        print('点击了---->>>>：${newList[index]}');
        Navigator.push(context, MaterialPageRoute(
            builder: (context) {
              return AdminDetailPage(newList[index]);
            }
        ));
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
          border: Border.all(width: 1,color: Colors.black12),
          borderRadius: BorderRadius.all(Radius.circular(6))
    ),
        child: Row(
          children: <Widget>[
            _leftOrderNumber(newList,index),
            _rightContent(newList,index),
          ],
        ),
      ),
    );
  }

  Widget _leftOrderNumber(newList,index) {
    return Container(
      margin: EdgeInsets.only(right: 10),
      padding: EdgeInsets.only(left: 6),
      width: 50,
      child: Column(
        children: <Widget>[
          Text('序号${index}',
            textAlign: TextAlign.center,

          ),
        ],
      ),
    );
  }

  Widget _rightContent(newList,index) {
    return Container(
      padding: EdgeInsets.only(left: 8,right: 6,top: 6,bottom: 6),
      decoration: BoxDecoration(
          border: Border(
            left: BorderSide(color: Colors.grey,width: 1),
          )
      ),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '用户id:',
                ),
              ),
              Container(
                width: 160,
                  child: Text('${newList[index].id}'),
                ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '用户账号:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${newList[index].userAccount}'),
              ),
            ],
          ),
        ],
      ),
    );
  }


}



